<?php

include_once('top.php');

$project = mysql_real_escape_string($_GET['project']);
$milestone = mysql_real_escape_string($_GET['milestone']);
$task = mysql_real_escape_string($_GET['task']);

$task_info = fetch_only("SELECT `Milestone`, `Name`, `Description`, `Due_date`, `Employee`, `Description`, `Open` FROM `TASK` WHERE `Name`='$task' AND `Milestone`='$milestone' AND `Project`='$project'");
$user = fetch_only("SELECT `First_name`, `Last_name` FROM `USER` WHERE `Email`='$email'");

$today = date('m/d/Y');

if (empty($_POST)) {
	$date = $today;
} else {
	$date = mysql_real_escape_string($_POST['date']);
	$hours = mysql_real_escape_string($_POST['hours']);
	if (strtotime($date) > time()) {
		$message = 'You cannot log future work!';
	} else {
		if ($hours > 8) {
			$message = 'Cannot enter more than 8 hours';
		} else {
			$date = date('Y-m-d', strtotime($date));
			$currentHours = fetch_only("SELECT * FROM ENTRY WHERE Date='$date' AND Milestone='$milestone' AND Task='{$task_info['Name']}' AND Project=$project;");
			
			if (empty($currentHours)){
				run("DELETE FROM `ENTRY` WHERE `Date`='$date' AND `Task`='$task' AND `Milestone`='$milestone' AND `Project`='$project'");
				run("INSERT INTO `ENTRY` (`User`, `Date`, `Task`, `Hours`, `Milestone`, `Project`) VALUES ('$email', '$date', '$task', '$hours', '$milestone', '$project')");
				header('location: dashboard.php?hours_saved=1');
			} else {
				$newHours = $currentHours['Hours'] + $hours;
				if ($newHours > 8){
					$message = 'Cannot enter more than 8 hours';
				} else {
					run("UPDATE ENTRY SET Hours=$newHours WHERE Date='$date' AND Milestone='$milestone' AND Task='{$task_info['Name']}' AND Project=$project;");
					header('location: dashboard.php?hours_saved=1');
				}
			}
			$date = $today;
			$hours = '';
		}
	}
}
?>

<html>
<head>
	<title>GT Project Manager - Time Tracking</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
</head>
<body>
	<form action="#" method="post">
		<table cellspacing="0" cellpadding="0" id="main">
			<tr><td id="title">
				Time Tracking
				<div class="nav">
					<a href="login.php?action=logout">Log Out</a><br />
					<?php echo $link ?><br />
					<a href="task_info.php?project=<?php echo $project ?>&milestone=<?php echo $milestone ?>&name=<?php echo $task ?>">Task Info</a>
				</div>
			</td></tr>
			<?php
			if ($message) {
			?>
			<tr><td class="message" colspan="2"><?php echo $message ?></td></tr>
			<?php
			}
			?>
			<tr>
				<td>
					<table class="details" cellspacing="0" cellpadding="0">
						<tr>
							<th>Milestone</th>
							<td><?php echo $milestone ?></td>
						</tr>
						<tr>
							<th>Task</th>
							<td><?php echo $task ?></td>
						</tr>
						<tr>
							<th>Description</th>
							<td class="description">
								<?php echo $task_info['Description'] ?>
							</td>
						</tr>
						<tr>
							<th>Employee</th>
							<td>
								<?php echo "{$user['First_name']} {$user['Last_name']}" ?>
							</td>
						</tr>
						<tr>
							<th>Date</th>
							<td class="date"><input type="text" name="date" value="<?php echo $date ?>" /></td>
						</tr>
						<tr>
							<th>Hours</th>
							<td><input type="text" name="hours" style="width: 40px" value="<?php echo $hours ?>" /></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td class="button_bar">
					<input type="submit" value="Save" />
				</td>
			</tr>
		</table>
	</form>
</body>
</html>